<template>
  <el-dialog :visible.sync="dialog" :title="isAdd ? '新增权限' : '编辑权限'" append-to-body width="600px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
      <el-form-item label="权限名称" prop="busMainData.privName">
        <el-input v-model="form.busMainData.privName" placeholder="名称" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="权限编码" prop="busMainData.privCode">
        <el-input v-model="form.busMainData.privCode" placeholder="权限编码" style="width: 460px;"/>
      </el-form-item>
      <el-form-item label="是否管理员" prop="busMainData.admin">
        <el-radio-group v-model="form.busMainData.admin">
          <el-radio v-for="item in dicts.yesorno" :label="item.dictCode === 'true'" :key="item.dictCode" >{{ item.dictName }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="上级权限">
        <treeselect v-model="form.busMainData.parentId" :options="privs" style="width: 460px;" placeholder="选择上级权限" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOrEdit, getAllPrivs, viewMainData } from '@/api/priv'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Treeselect },
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      required: true
    },
    dicts: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      privs: [],
      form: {
        busMainData: {
          id: null,
          privName: '',
          privCode: '',
          admin: false,
          enableFlag: 'Y'
        },
        busCommonState: {
          selfState: '0'
        }
      },
      rules: {
        'busMainData.privName': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ],
        'busMainData.privCode': [
          { required: true, message: '请输入代码', trigger: 'blur' },
          { min: 2, max: 100, message: '长度在 2 到 100 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    viewMainData(id) {
      const data = { paramBean: { id: id }}
      viewMainData(data).then(res => {
        this.form = res.data
      })
    },
    doSubmit() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      addOrEdit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
    },
    initDataOfBefore() {
      getAllPrivs().then(res => {
        this.privs = res.data
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  /deep/ .el-input-number .el-input__inner {
    text-align: left;
  }
</style>
